/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>
 * 
 * 
 */


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdio>
#include <gtk/gtk.h>

using namespace std;

ifstream namein("name.list");
ofstream nout("name.list", ios::app);

vector<string> names;
int nsum = 0;
string uti;
GtkWidget *entn, *ents;

GdkPixbuf *create_pixbuf(const gchar* filename)
{
	GdkPixbuf *pixbuf;
	GError *error = NULL;
	pixbuf = gdk_pixbuf_new_from_file(filename, &error);
	if(!pixbuf) {
		fprintf(stderr, "%s\n", error->message);
		g_error_free(error);
	}
	return pixbuf;
}

void endp(GtkWidget *wid, gpointer ptr)
{
    gtk_main_quit();
}

void newc(GtkWidget *wid, gpointer ptr)
{
    const char *name;
    char out[20];
    name = gtk_entry_get_text(GTK_ENTRY(entn));
    nout << name << endl;
    sprintf(out, "客户编号: %i", nsum);
    gtk_label_set_text(GTK_LABEL(ptr), out);
    uti = name;
    names.push_back(uti);
    nsum++;
}
void verc(GtkWidget *wid, gpointer ptr)
{
    const char *name;
    int sern = atoi(gtk_entry_get_text(GTK_ENTRY(ents)));
    char out[20];
    name = gtk_entry_get_text(GTK_ENTRY(entn));
    string tmp_name(name);
    if(sern < nsum && (names[sern] == name || names[sern] == tmp_name + "\n"))
    {
        sprintf(out, "客户存在");
    }
    else
    {
        sprintf(out, "客户不存在");
    }
    gtk_label_set_text(GTK_LABEL(ptr), out);
}

void badcom(char **argv)
{
    cerr << "Usage: " << argv[0] <<" [n|v|new|verify] [<name>] [<num>]" << endl;
}

int main(int argc, char **argv)
{
    cout << "Reading...  ";
    while(getline(namein, uti))
    {
        nsum++;
        names.push_back(uti);
    }
    cout << "Done." << endl;
    if(argc > 1)
    {
        string oper;
        oper = argv[1];
        if(argc == 2)
        {
            badcom(argv);
            return 1;
        }
        if(oper == "new" || oper == "n")
        {
            if(argc != 3)
            {
                badcom(argv);
                return 2147483647;
            }
            nout << argv[2] << '\n';
            cout << "客户编号为：" << nsum << endl;
            return 0;
        }
        if(oper == "verify" || oper == "v")
        {
            if(argc != 4)
            {
                badcom(argv);
                return 2147483647;
            }
            string ns = argv[3];
            stringstream sns(ns);
            int n;
            sns >> n;
            uti = argv[2];
            if(n < nsum && names[n] == uti)
            {
                cout << "[OK] 客户存在。" << endl;
                return 0;
            }
            else
            {
                cout << "[!!] 客户不存在。" << endl;
                return 0;
            }
        }
        badcom(argv);
        return 222;
    }
    gtk_init(&argc, &argv);
    GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(win),"CCVT+");
    gtk_window_set_icon(GTK_WINDOW(win), create_pixbuf("/usr/share/ccvt_p/ccvt.ico"));
    GtkWidget *tab = gtk_table_new(3, 4, TRUE);
    GtkWidget *lbln= gtk_label_new("姓名: ");
    GtkWidget *lbls= gtk_label_new("编号: ");
    GtkWidget *lblo= gtk_label_new("");
    GtkWidget *btnn= gtk_button_new_with_label("新增");
    GtkWidget *btnv= gtk_button_new_with_label("验证");
    entn = gtk_entry_new();
    ents = gtk_entry_new();
    g_signal_connect(win, "delete_event", G_CALLBACK(endp), NULL);
    g_signal_connect(btnn,"clicked",      G_CALLBACK(newc), lblo);
    g_signal_connect(btnv,"clicked",      G_CALLBACK(verc), lblo);
    gtk_table_attach_defaults(GTK_TABLE(tab), lbln, 0, 1, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(tab), entn, 1, 3, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(tab), btnn, 3, 4, 0, 1);
    gtk_table_attach_defaults(GTK_TABLE(tab), lbls, 0, 1, 1, 2);
    gtk_table_attach_defaults(GTK_TABLE(tab), ents, 1, 3, 1, 2);
    gtk_table_attach_defaults(GTK_TABLE(tab), btnv, 3, 4, 1, 2);
    gtk_table_attach_defaults(GTK_TABLE(tab), lblo, 0, 4, 2, 3);
    gtk_container_add(GTK_CONTAINER(win), tab);
    gtk_widget_show_all(win);
    gtk_main();
    return 0;
}
